define(['vue', 'common'], function (Vue, common) {
    const {createApp} = Vue;
    const Controller = {
        index: function () {
            let app = createApp({
                data() {
                    return {
                        groupList: [],
                        groupAction: '',
                        list: null
                    }
                },
                created() {
                    this.groupList = Config.group
                    // console.log(this.groupList,'ggggggggggggggg')
                    this.groupAction = this.groupList[0].key
                    let newlist = {}
                    for (let i in Config.group) {
                        newlist[Config.group[i].key] = []
                    }
                    this.list = newlist
                    // console.log(this.list,'llllllllllllllllll')
                    // this.getList()
                    this.$nextTick(function () {
                        this.getList()
                    })
                },
                methods: {
                    ...common,
                    removeConfig(item) {
                        // console.log(item)
                        let that = this;
                        let url = common.getHandleUrl(`del`);
                        common.ElMessageBox(`确定要删除${item.title}吗?`, function () {
                            common.postHandle(url, {ids: item.id}).then(res => {
                                that.success("删除成功", () => {
                                    that.getList(item.group)
                                })
                            })
                        })
                    },
                    saveConfig(item) {
                        console.log(item)
                        let list = common.copy(this.list[item.key])
                        let url = common.getHandleUrl('edit');
                        let that = this;
                        // console.log(list)
                        // return
                        common.postHandle(url, {row: list}).then(res => {
                            that.getList(item.key)
                            list.forEach(itemss => {
                                if (itemss.name === 'theme_color') {
                                    if (parent.my_changeTheme) {
                                        parent.my_changeTheme(itemss.value)
                                    }
                                }
                            })
                        })
                    },
                    addConfig(item) {
                        console.log(item)
                        let url = common.getHandleUrl(`add?group=${item.key}`);
                        console.log(url)
                        let that = this
                        common.open('添加配置', url,['600px','95%'], function (layero, index) {
                            that.getList()
                        })
                    },
                    removeTab(e) {
                        let groupList = this.groupList;
                        let index = groupList.findIndex(item => item.key == e)
                        let that = this
                        // console.log(e,this.groupAction)
                        let isthis = e == that.groupAction ? true : false
                        let url = common.getHandleUrl('del_group')
                        common.ElMessageBox(`确定要删除${groupList[index].title}吗?`, function () {
                            common.postHandle(url, {key: e}).then(res => {
                                that.success("删除成功", () => {
                                    that.getGroupList()
                                    if (isthis) {
                                        that.groupAction = that.groupList[0].key
                                    }
                                })
                            })
                        })
                    },
                    async getList(key = '') {
                        let groupList = common.copy(this.groupList)
                        let that = this
                        if (key) {
                            common.post(Config.sub_url, {group: key}).then(res => {
                                if (res.code == 1) {
                                    that.list[key] = res.data[key]
                                }
                            })
                            return
                        }
                        let groups = []
                        for (let i in groupList) {
                            groups.push(groupList[i].key)
                        }
                        groups = groups.join(',')
                        common.post(Config.sub_url, {group: groups}).then(res => {
                            if (res.code == 1) {
                                // console.log(res.data)
                                that.list = res.data
                            }
                        })

                        // console.log(list)
                        // this.list = list
                        // console.log(this.list)

                    },
                    getGroupList() {
                        let that = this;
                        common.postHandle(common.getHandleUrl('get_group_list')).then(res => {
                            that.groupList = res;
                        })
                    },
                    handleAddGroup() {
                        let that = this
                        common.open('添加分组', common.getHandleUrl('add_group'), function (layero, index) {
                            that.getGroupList()
                        })
                    }
                }
            })
            common.load(app, ['all'])
            app.mount('#app')
        },
        add_group: function () {
            let app = Vue.createApp({
                data() {
                    return {
                        form: {
                            title: '',
                            key: '',
                            status: 1
                        },
                        rules: {
                            title: [
                                {required: true, message: '请输入分组名称', trigger: 'blur'}
                            ],
                            key: [
                                {required: true, message: '请输入分组标识', trigger: 'blur'}
                            ]
                        }
                    }
                },
                methods: {
                    ...common,
                    onSubmitAdd() {
                        // console.log(this.form)
                        let data = {
                            row: common.copy(this.form)
                        }
                        let that = this
                        common.postHandle(Config.sub_url, data, true).then(res => {
                            that.success("添加成功", () => {
                                common.close()
                            })
                        })
                    }
                }
            })
            common.load(app, ['icon'])
            app.mount('#app')
        },
        add: function () {
            let app = Vue.createApp({
                data() {
                    return {
                        form: {
                            name: '',
                            group: Config.group,
                            title: '',
                            tip: '',
                            type: 'text',
                            value: '',
                            content: '',
                            status: 1
                        },
                        formTypeList: Config.form_type,
                        sxList: [
                            'radio',
                            'checkbox',
                            'select',
                            'selects',
                            'cascader',
                            'cascaders',
                        ],
                        rules: {
                            title: [
                                {required: true, message: '请输入配置名称', trigger: 'blur'}
                            ],
                            name: [
                                {required: true, message: '请输入配置标识', trigger: 'blur'}
                            ]
                        }
                    }
                },
                created() {
                    console.log(Config,'cccccccccccccccc')
                },
                methods: {
                    ...common,
                }
            })
            common.load(app, ['all'])
            app.mount('#app')
        }
    }
    return Controller;
})